package cn.iocoder.yudao.module.mp.service.handler.other;

import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.mp.convert.menu.MpMenuConvert;
import cn.iocoder.yudao.module.mp.framework.mp.core.context.MpContextHolder;
import cn.iocoder.yudao.module.mp.service.message.MpMessageService;
import cn.iocoder.yudao.module.mp.service.message.bo.MpMessageSendOutReqBO;
import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindSimpleReqDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 扫码的事件处理器
 */
@Component("MpScanHandler")
@Slf4j
public class ScanHandler implements WxMpMessageHandler {

    @Resource
    @Lazy // 延迟加载，解决循环依赖的问题
    private SocialUserApi socialUserApi;

    @Resource
    private MpMessageService mpMessageService;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> context,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        //
        SocialUserBindSimpleReqDTO socialUserBindReqDTO = new SocialUserBindSimpleReqDTO();
        Long userId = NumberUtil.parseLong(wxMpXmlMessage.getEventKey());
        TenantUtils.executeIgnore(()->{
            AdminUserRespDTO user = adminUserApi.getUser(userId);
            TenantUtils.execute(user.getTenantId(),()->{
                socialUserBindReqDTO.setUserId(userId);
                socialUserBindReqDTO.setUserType(UserTypeEnum.ADMIN.getValue());
                socialUserBindReqDTO.setSocialType(SocialTypeEnum.WECHAT_MP.getType());
                socialUserBindReqDTO.setCode("code");
                socialUserBindReqDTO.setState("");
                socialUserBindReqDTO.setOpenid(wxMpXmlMessage.getFromUser());
                socialUserApi.simpleBindSocialUser(socialUserBindReqDTO);
            });
        });
        // 第二步，回复消息
        MpMessageSendOutReqBO sendReqBO = new MpMessageSendOutReqBO();
        sendReqBO.setContent("绑定成功");
        sendReqBO.setAppId(MpContextHolder.getAppId());
        sendReqBO.setOpenid(wxMpXmlMessage.getFromUser());
        sendReqBO.setType(WxConsts.XmlMsgType.TEXT);
        return mpMessageService.sendOutMessage(sendReqBO);
    }

}
